var exam = {};
	exam.target = [];
	exam.time_id = [];
	exam.timeout = null;
	exam.timeinterval  = null;
	exam.clearTime =false;
	exam.openConfirm = false;
(function(){
	//倒计时
	exam.showdatetime = function(){ 
		exam.timeout = setTimeout("exam.showdatetime()", 1000); 
		for (var i=0,j=exam.target.length;i<j;i++){ 
		//程序执行到现在的时间
	            var NowTime = new Date().getTime();
	            //程序执行到现在过去的时间
	            var PastTime = NowTime - Record_start_time;
	            
	            //考试时间 - 过去的时间	            
	            var syTime = exam.target[i]  - PastTime;
                
                today=new Date();
                //考试剩下的时间
                timeold=syTime; 
				sectimeold=timeold/1000; 
				secondsold=Math.floor(sectimeold); 
				msPerDay=24*60*60*1000; 
				e_daysold=timeold/msPerDay; 
				daysold=Math.floor(e_daysold); 
				e_hrsold=(e_daysold-daysold)*24; 
				hrsold=Math.floor(e_hrsold); 
				e_minsold=(e_hrsold-hrsold)*60; 
				minsold=Math.floor((e_hrsold-hrsold)*60); 
				seconds=Math.floor((e_minsold-minsold)*60); 
		if (timeold<=0) { 
			//alert("考试时间到，系统将自动交卷。");
			
			exam.clearTime = true;
			window.clearTimeout(exam.timeout)
			window.clearInterval(exam.timeinterval)
		    document.getElementById(exam.time_id[i]).innerHTML="考试时间到，禁止答题！"; 
		    $("#noticeBox").html("考试时间已到，禁止答题！<br/> <br/>系统正在交卷..")
		   	$("#notice").show().animate( { top: "200"}, 500 )
			$("#mask").show();
			EXAM.sendExam(false)
		}  
		else { 
		    if (daysold<10) {daysold="0"+daysold} 
		    if (daysold<100) {daysold="0"+daysold} 
		    if (hrsold<10) {hrsold="0"+hrsold} 
		    if (minsold<10) {minsold="0"+minsold} 
		    if (seconds<10) {seconds="0"+seconds} 
		    if (daysold<3) { 
		        document.getElementById(exam.time_id[i]).innerHTML="<font color=red>"+hrsold+"小时"+minsold+"分"+seconds+"秒</font>"; 
		    }  
		    else { 
		        document.getElementById(exam.time_id[i]).innerHTML=daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒"; 
		    } 
		} 
		} 	
		}
		
		
		//答题
		exam.test = function(){//题型，答案
				this.type = -1;
				this._keyboardEvents()
			}
			exam.test.prototype = {
				
				loadExam:function(url){
					var _url = (url.indexOf("?")!="-1")?url+"&random="+Math.random():url+"?random="+Math.random()
					$.get(_url, function(data){
  							$("#content").html(data);
					}); 
				},
				highLightOld:function(idarray){
					$("td").removeClass("old")
					$.each(idarray,function(i){
							$("#exam_"+idarray[i]).addClass("old")
						})
					
				},
				highLightThis:function(id){
					$("td").removeClass("this");
					$("#exam_"+id).addClass("this")
				},
				gotoExam:function(type){
					this.loadExam("fillAnswer.action?to="+type+"&index="+$('#indexinput').val()+"&answer="+$('#answerinput').val())
				},
				setExamType:function(type){//1 判断 2 单选 3 多选
					this.type = type;
					//this._keyboardEvents()
				},
				loadAnswer:function(s){
					var _array = s.split("");
					var _s = "";
					var _v = "";
					$.each(_array,function(i){
							$("#exam"+_array[i]).addClass("hover");
							_s += _array[i]
							if(_array[i]=="1"){
							_v += "√"
							}
							else if(_array[i]=="2"){
							_v += "×"
							}
							else{
							_v += _array[i]
							}
						})
					$("#answer").html("["+_v+"]")
					$("#answerinput").val(_s)
				},
				setAnswer:function(s){
					var _this = this;
					if(this.type ==3){//多选
							$("#exam"+s).addClass("hover");
						var selectAns1 = ""
						$("#subBar .hover").each(function(i){
								selectAns1 += $(this).attr("value")
							})
						$("#answer").html("["+selectAns1+"]")
						$("#answerinput").val(selectAns1)
					}else{
					$("#subBar span").removeClass("hover")
					$("#exam"+s).addClass("hover");
					var selectAns = ""
					if(s=="2"){
						selectAns = "×"
					}
					else if(s=="1"){
						selectAns ="√"
					}
					else{
						selectAns = s;
					}
					$("#answer").html("["+selectAns+"]")
					$("#answerinput").val(s)
					if(document.getElementById("ne")){
							_this.gotoExam($("#ne").attr("avalue"))
							}
					}
				},
				rewriteAnswer:function(){
					$("#subBar span").removeClass("hover")
					$("#answer").html("[]")
					$("#answerinput").val("")
				},
				sendExam:function(t){
					if(t){
						this.openconfirm()
					//	if()
						/*if(window.confirm("你确定要交卷吗？")){
							exam.clearTime = true;
							window.clearTimeout(exam.timeout)
							window.clearInterval(exam.timeinterval)
							$("#noticeBox").html("系统正在交卷..")
						   	$("#notice").show().animate( { top: "200"}, 500 )
							$("#mask").show();
							$("#sendForm").submit()
							}*/				
						}
						else{
						$.unblockUI(); 
						exam.openConfirm = false;
						exam.clearTime = true;
						window.clearTimeout(exam.timeout)
						window.clearInterval(exam.timeinterval)
						//交卷的时候将当前所做题目保存
						$("#sendForm").append($("#answerinput")).append($("#indexinput"))
						$("#sendForm").submit()
						}
				},
				sendExamnMsg:function(string){
					$("#noticeBox").html($.trim(string))
				},
				_keyboardEvents:function(){
					var _this = this;
					var rewrite = 106;//*重做
					var submit = 109;//-交卷
					var pr = 107;//+上一题
					var nex = 13//enter 下一题
					var A = 97;
					var B = 98;
					var C = 99;
					$(document).unbind();
					$(document).keydown(function(e){//取消浏览器回退
						if(!external){
							return false;
						}
						})
					$(document).keyup(function(e){
						if(e.keyCode==8) return false;
						if(exam.clearTime) return false;
						if(e.keyCode == submit){
							if(exam.openConfirm){
							_this.closeconfirmSubmit()
							}else{
							_this.sendExam(true)
							}
							
						}
						else if(e.keyCode==rewrite){
							if(exam.openConfirm){
								return false;
							}
							else{
							_this.rewriteAnswer()
							}
						}
						else if(e.keyCode == nex){
							if(exam.openConfirm){//点击了交卷按钮
								return false;
							//	_this.closeconfirmSubmit()
							}else{
								if(document.getElementById("ne")){
								_this.gotoExam($("#ne").attr("avalue"))
								}
							}
						}
						else if(e.keyCode == pr){
							if(exam.openConfirm){//点击了交卷按钮
								_this.closeconfirmNOSubmit()
							}else{
								if(document.getElementById("pr")){
								_this.gotoExam($("#pr").attr("avalue"))
							}
							}
						}
						else if(e.keyCode == A){
							if(exam.openConfirm){
								return false;
							}
							else{
							if(_this.type==1){//选择题
								_this.setAnswer("1")
							}
							else{
								_this.setAnswer("A")
							}
							}
						}
						else if(e.keyCode == B){
							if(exam.openConfirm){
								return false;
							}
							else{
							if(_this.type==1){//选择题
								_this.setAnswer("2")
							}
							else{
								_this.setAnswer("B")
							}
							}
						}
						else if(e.keyCode == C){
							if(exam.openConfirm){
								return false;
							}
							else{
							if(_this.type==1){//选择题
								
							}
							else{
								_this.setAnswer("C")
							}
							}
						}
					})
				},
				stimeout:function(url,time){
				exam.timeinterval = window.setInterval(function(){
  					$.get(url+"?random="+Math.random(), function(data){
  							
					});
				  },time);
			},
			 openconfirm:function(){
			 	$.blockUI({ message: $('#Customconfirm'), css: { width: '500px',marginLeft:'-200px' } }); 
			 	setTimeout(function(){
			 		$("#yes").blur()
			 		},100)
			 	exam.openConfirm = true;
			 },
			closeconfirmSubmit:function(){//确定交卷
				$.unblockUI(); 
				exam.openConfirm = false;
				exam.clearTime = true;
				window.clearTimeout(exam.timeout)
				window.clearInterval(exam.timeinterval)
				$("#noticeBox").html("系统正在交卷..")
			   	$("#notice").show().animate( { top: "200"}, 500 )
				$("#mask").show();
				//交卷的时候将当前所做题目保存
				$("#sendForm").append($("#answerinput")).append($("#indexinput"))	
				$("#sendForm").submit()
			},
			closeconfirmNOSubmit:function(){//取消交卷
				$.unblockUI(); 
				exam.openConfirm = false;
			}
			}
	})()
exam.timeout = setTimeout("exam.showdatetime()", 100); 